package com.atguigu.daijia.customer.service.impl;

import cn.binarywang.wx.miniapp.api.WxMaService;
import cn.binarywang.wx.miniapp.bean.WxMaJscode2SessionResult;
import com.atguigu.daijia.customer.mapper.CustomerInfoMapper;
import com.atguigu.daijia.customer.mapper.CustomerLoginLogMapper;
import com.atguigu.daijia.customer.service.CustomerInfoService;
import com.atguigu.daijia.model.entity.customer.CustomerInfo;
import com.atguigu.daijia.model.entity.customer.CustomerLoginLog;
import com.atguigu.daijia.model.vo.customer.CustomerLoginVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.micrometer.common.util.StringUtils;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.common.error.WxErrorException;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

@Slf4j
@Service
@SuppressWarnings({"unchecked", "rawtypes"})
public class CustomerInfoServiceImpl extends ServiceImpl<CustomerInfoMapper, CustomerInfo> implements CustomerInfoService {

    @Resource
    WxMaService wxMaService;

    @Resource
    CustomerInfoMapper customerInfoMapper;

    @Resource
    CustomerLoginLogMapper customerLoginLogMapper;


    @Override
    public Long login(String code) {
        //1.获取code值,使用微信工具包对象,获取微信唯一标识openid
        String openId = null;
        try {
            WxMaJscode2SessionResult sessionInfo = wxMaService.getUserService().getSessionInfo(code);

            openId = sessionInfo.getOpenid();

        } catch (WxErrorException e) {
            throw new RuntimeException(e);
        }
        //根据openid,判断是否第一次登录
        //如果openId不存在,返回null,如果存在,返回只有一条记录
        LambdaQueryWrapper<CustomerInfo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(CustomerInfo::getWxOpenId, openId);
        CustomerInfo customerInfo = customerInfoMapper.selectOne(queryWrapper);
        if (customerInfo == null) {
            //第一次登录,添加信息到用户表
            customerInfo = new CustomerInfo();
            customerInfo.setNickname(String.valueOf(System.currentTimeMillis()));
            customerInfo.setAvatarUrl("https://oss.aliyuncs.com/aliyun_id_photo_bucket/default_handsome.jpg");
            customerInfo.setWxOpenId(openId);
            customerInfoMapper.insert(customerInfo);
        }
        //记录登录日志
        CustomerLoginLog customerLoginLog = new CustomerLoginLog();
        customerLoginLog.setCustomerId(customerInfo.getId());
        customerLoginLog.setMsg("小程序登录");
        customerLoginLogMapper.insert(customerLoginLog);
        //返回用户id值

        return customerInfo.getId();
    }

    @Override
    public CustomerLoginVo getCustomerLoginInfo(Long customerId) {
        //根据用户id查询用户信息

        CustomerInfo customerInfo = customerInfoMapper.selectById(customerId);

        //封装到CustomerLoginVo对象中
        CustomerLoginVo customerLoginVo = new CustomerLoginVo();
//        customerLoginVo.setNickname(customerInfo.getNickname());
        BeanUtils.copyProperties(customerInfo, customerLoginVo);
        String phone = customerInfo.getPhone();
        //判读是否需要手机号
        boolean isBindPhone = StringUtils.isEmpty(phone);
        customerLoginVo.setIsBindPhone(isBindPhone);

        //CustomerLoginVo 返回

        return customerLoginVo;
    }

    @Override
    public String getCustomerOpenId(Long customerId) {
        LambdaQueryWrapper<CustomerInfo> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(CustomerInfo::getId, customerId);
        CustomerInfo customerInfo = customerInfoMapper.selectOne(wrapper);
        return customerInfo.getWxOpenId();

    }
}
